package dr.server.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import dr.common.ResultModel;
import dr.common.vo.PusherMessage;
import dr.server.impl.announce.AnnounceService;
import dr.server.impl.jpush.JPusherService;
import dr.server.impl.mail.JMailService;
import dr.server.impl.sms.aliyun.AliyunSmsService;
import dr.server.impl.sms.submail.SubMailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class PusherService {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    AliyunSmsService aliyunSmsService;

    @Autowired
    SubMailService subMailService;

    @Autowired
    JPusherService jPusherService;

    @Autowired
    JMailService jMailService;

    @Autowired
    AnnounceService announceService;

    @Autowired
    AstrictService astrictService;

    /**
     * 解析消息
     * type： 消息类型（0=不发送 1=阿里云短信 2=赛迪短信 3=极光推送 8=新版阿里云短信发送)
     * to: 发给谁
     * msg： 发送内容或者模板
     * param: 参数
     *
     * @param message 消息信息
     */
    public void doPush(PusherMessage message) {
        ResultModel resultModel = new ResultModel();
        String type = message.getType();
        String to = message.getTo();
        String messageOrTemplate = message.getContent();
        String param = message.getJsonParam();
        String msgId = message.getMsgId();
        msgId = doGetOrSaveMsgId(msgId, 1);

        if(astrictService.check(type,to)){
            return;
        }
        if ("1".equals(type)) {
            resultModel = aliyunSmsService.sendSms(to, messageOrTemplate, param);
        }
        if ("2".equals(type)) {
            resultModel = subMailService.sendSms(to, messageOrTemplate, param);
        }
        if ("3".equals(type)) {
            String title = message.getTitle();
            String cronTime = message.getCronTime();
            String msgType = message.getMsgType();
            resultModel = jPusherService.push(msgType, title, to, messageOrTemplate, cronTime, param);
        }
        if ("4".equals(type)) {
            String title = message.getTitle();
            String filepath = message.getFilepath();
            String msgType = message.getMsgType();
            resultModel = jMailService.send(msgType, title, to, messageOrTemplate, filepath);
        }

        if ("5".equals(type)) {
            String title = message.getTitle();
            String msgType = message.getMsgType();
            String url = message.getUrl();
            resultModel = announceService.publish(msgType, title, to, messageOrTemplate, url);
        }

        if ("8".equals(type)) {
            resultModel = aliyunSmsService.sendSmsByKey(to, messageOrTemplate, param);
        }


        if(!resultModel.success()) {
            log.error("发送消息失败：{}", JSONObject.toJSONString(resultModel));
            doGetOrSaveMsgId(msgId, 3);
        }else {
            doGetOrSaveMsgId(msgId, 2);
        }
    }

    private String doGetOrSaveMsgId(String msgId, int type) {
        if(type == 1) {
            if(StrUtil.isEmpty(msgId)) {
                msgId = IdUtil.objectId();
            }

            String key = "pusher:message:" + msgId;
            if(StrUtil.isEmpty(stringRedisTemplate.opsForValue().get(key))) {
                stringRedisTemplate.opsForValue().set(key, "0", 60 , TimeUnit.DAYS);
            }
        }

        if(type == 2) {
            String key = "pusher:message:" + msgId;
            if(StrUtil.isNotEmpty(stringRedisTemplate.opsForValue().get(key))) {
                stringRedisTemplate.opsForValue().set(key, "1");
            }
        }


        if(type == 2) {
            String key = "pusher:message:" + msgId;
            if(StrUtil.isNotEmpty(stringRedisTemplate.opsForValue().get(key))) {
                stringRedisTemplate.opsForValue().set(key, "2");
            }
        }
        return msgId;
    }
}
